package MIU.TwistedLabirynth.Levels;

import static MIU.TwistedLabirynth.menu.SettingsMenu.LEVEL_HARD;
import static MIU.TwistedLabirynth.menu.SettingsMenu.LEVEL_MEDIUM;
import static MIU.TwistedLabirynth.pac.GameLogicController.CAMERA_HEIGHT;
import static MIU.TwistedLabirynth.pac.GameLogicController.CAMERA_WIDTH;
import static org.anddev.andengine.extension.physics.box2d.util.constants.PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.audio.music.Music;
import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.modifier.PathModifier.Path;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import MIU.TwistedLabirynth.R;
import MIU.TwistedLabirynth.Shapes.CarouselManager;
import MIU.TwistedLabirynth.Shapes.EnemyManager;
import MIU.TwistedLabirynth.Shapes.ShapeDirection;
import MIU.TwistedLabirynth.Shapes.SpikesManager;
import MIU.TwistedLabirynth.Shapes.SwordManager;
import MIU.TwistedLabirynth.Shapes.TrampolineManager;
import MIU.TwistedLabirynth.pac.GameLogicController;
import MIU.TwistedLabirynth.pac.Player;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.EditText;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;

public class LevelController {

	private final ArrayList<Shape> enemyList;
	private final ArrayList<Shape> goodsList;
	private final ArrayList<Shape> endPointList;
	private final GameLogicController gameLogicController;
	private final FixtureDef wallFixtureDefF;

	private Player mPlayer;
	private Body mPlayerBody;

	private int levelId;
	
	private boolean isGameFinished = false;
	
	private Music gameMusic;
	private Sound gameOverSound;
	
	private ChangeableText scoreText;
	private int gameTime = 60000;
	private IUpdateHandler timeHandler;


	// Level sign textures
	private BitmapTextureAtlas mLevelSignTextures;
	private TextureRegion mLevelCompletedRegion;
	private TextureRegion mLevelUnCompletedRegion;

	public LevelController(GameLogicController gameLogicController) {
		this.levelId = 0;
		
		this.gameLogicController = gameLogicController;
		this.enemyList = new ArrayList<Shape>();
		this.goodsList = new ArrayList<Shape>();
		this.endPointList = new ArrayList<Shape>();
		this.wallFixtureDefF = PhysicsFactory.createFixtureDef(0, 0.0f, 0.0f);

		/* Make the Snake move every 0.2 seconds. */
		timeHandler = new TimerHandler(0.011f, true, new ITimerCallback() {
			@Override
			public void onTimePassed(final TimerHandler pTimerHandler) {
				gameTime -= 10;
				scoreText.setText("Time: " + gameTime);
			}
		});
	}
	
	public void loadResources() {
		mLevelSignTextures = new BitmapTextureAtlas(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mLevelCompletedRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelSignTextures,
				gameLogicController, "ok_button.png", 0, 0);
		mLevelUnCompletedRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mLevelSignTextures,
				gameLogicController, "fail_button.png", 0, 64);
		gameLogicController.getEngine().getTextureManager().loadTextures(this.mLevelSignTextures);
	}

	public void setCurrentLevel(int levelId){
		this.levelId = levelId;
	}
	
	
	public void showSignCompleted() {
		gameLogicController.getScene().unregisterUpdateHandler(timeHandler);
		clearShapesLists();
		Shape box = new Sprite(160, 120, 100, 100, mLevelCompletedRegion)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY)
			{
				if (gameLogicController.checkTouchTime())
				{
					gameLogicController.getPlayerProfileManager().incraseUnlockedLevelNumber(
							gameLogicController.levelController.levelId);
					gameLogicController.setLevelMenuScene();

					final AlertDialog.Builder alert = new AlertDialog.Builder(gameLogicController);
					final EditText input = new EditText(gameLogicController);
					alert.setView(input);
					alert.setTitle("HIGHSCORE");
					alert.setIcon(R.drawable.trophy);
					alert.setMessage("Your score: " + gameTime + ". Put name:");
					alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							String value = input.getText().toString().trim();
							gameLogicController.getHighscoreDB().createRow(value, gameTime);
						}
					});

					alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							dialog.cancel();
						}
					});
					alert.show();

				}
				return true;
			}
		};
		gameLogicController.getScene().registerTouchArea(box);
		gameLogicController.getScene().attachChild(box);
	}
	
	public void showSignUncompleted() {
		gameLogicController.getScene().unregisterUpdateHandler(timeHandler);
		clearShapesLists();
		Shape box = new Sprite(160, 120, 100, 100, mLevelUnCompletedRegion)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY)
			{
				if (gameLogicController.checkTouchTime())
				{	
						gameLogicController.getEngine().setScene(gameLogicController.newGameLevelScene(levelId));
				}
				return true;
			}
		};
		gameLogicController.getScene().registerTouchArea(box);
		gameLogicController.getScene().attachChild(box);
	}
	
	public void callbackCollisionWithEndPoint(){

		if (goodsList.size() == 0)
		{
			if(!isGameFinished)
			{
				gameLogicController.runOnUpdateThread(new Runnable() {

					@Override
					public void run() {
						gameLogicController.getScene().detachChild(mPlayer);
					}
				});
				isGameFinished = true;
				showSignCompleted();
				if (gameMusic.isPlaying()) {
					gameMusic.pause();
				}
				if (gameLogicController.getSettingsMenu().isMusicEnabled()) {
					gameOverSound.play();
				}
			}
		}
	}
	
	public void callbackCollisionGoods(int i) {
		final Shape goodShape = goodsList.get(i);
		gameLogicController.runOnUpdateThread(new Runnable() {

			@Override
			public void run() {
				gameLogicController.getScene().detachChild(goodShape);
			}
		});

		goodsList.remove(i);
		
		checkGoodsList();
	}
	
	public void callbackCollisionEnemy(){
		//animacija
		//restart 
		gameLogicController.runOnUpdateThread(new Runnable() {

			@Override
			public void run() {
				gameLogicController.getScene().detachChild(mPlayer);
			}
		});
		isGameFinished = true;
		showSignUncompleted();
	}
	
		
	public void createPlayer(TiledTextureRegion mCircleFaceTextureRegion, int x, int y){
		mPlayer = new Player(x, y, 30, 30, mCircleFaceTextureRegion, this);
		FixtureDef FIXTURE = PhysicsFactory.createFixtureDef(0, 0.0f, 0.5f);
		mPlayerBody = PhysicsFactory.createCircleBody(gameLogicController.getPhysicsWorld(), mPlayer,
				BodyType.DynamicBody, FIXTURE);

		gameLogicController.getScene().attachChild(mPlayer);
		gameLogicController.getPhysicsWorld().registerPhysicsConnector(
				new PhysicsConnector(mPlayer, mPlayerBody, true, true));
	}
	
	
	public void createEndPoint(int x, int y, int sizeX, int sizeY){
		Sprite endPoint = new Sprite(x, y, sizeX, sizeY, gameLogicController.mFinishLineTextureRegion);
		gameLogicController.getScene().attachChild(endPoint);
		endPoint.setVisible(false);
		endPointList.add(endPoint);
	}
	
	public void loadLevel(int levelId){
		gameTime = 60000;
		createFrame();
		isGameFinished = false;
		this.setCurrentLevel(levelId);
		clearShapesLists();

		if(levelId == 1)
			loadLevel3_1();//loadLevel1();
		else if(levelId == 2)
			loadLevel2();
		else if(levelId == 3)
			loadLevel3();
		else if(levelId == 4)
			loadLevel4();
		else if(levelId == 5)
			loadLevel5();
		else if(levelId == 6)
			loadLevel6();
		else if(levelId == 7)
			loadLevel7();
		else if(levelId == 8)
			loadLevel8();
		else if(levelId == 9)
			loadLevel9();
		
		checkGoodsList();
	}
	
	private void checkGoodsList(){
		if(goodsList.isEmpty()){
			for(Shape endPoint : this.endPointList){ 
				endPoint.setVisible(true);
			} 
		}
	}

	public void addBoxShape(float x, float y, int height,int weight, TextureRegion textureRegion){
		Shape box = new Sprite(x, y, height, weight, textureRegion);
		PhysicsFactory.createBoxBody(gameLogicController.getPhysicsWorld(), box, BodyType.StaticBody, wallFixtureDefF);
		gameLogicController.getScene().attachChild(box);
	}
	
	public void addCarousel(float x, float y){
		CarouselManager carouselManager = new CarouselManager(x, y, 128, 128,
				gameLogicController.carouselTextureRegion);
		
		final float width = carouselManager.getWidthScaled() / PIXEL_TO_METER_RATIO_DEFAULT;
		final float height = carouselManager.getHeightScaled() / PIXEL_TO_METER_RATIO_DEFAULT;
		
		final Vector2[] vertices = {
				new Vector2(-0.50282f*width, -0.00087f*height),
				new Vector2(-0.05794f*width, -0.07682f*height),
				new Vector2(-0.00369f*width, -0.48915f*height)
		};
		
		final Vector2[] vertices2 = {
			new Vector2(-0.49601f*width, +0.00167f*height),
			new Vector2(-0.11422f*width, -0.08020f*height),
			new Vector2(-0.00572f*width, -0.50066f*height),
			new Vector2(+0.08923f*width, -0.07567f*height),
			new Vector2(+0.49613f*width, +0.00118f*height),
			new Vector2(+0.08018f*width, +0.05544f*height),
			new Vector2(+0.00333f*width, +0.48042f*height),
			new Vector2(-0.10066f*width, +0.07352f*height)
		};

		Body body = PhysicsFactory.createPolygonBody(gameLogicController.getPhysicsWorld(), carouselManager, vertices,
							BodyType.KinematicBody, wallFixtureDefF);
			
		body.setAngularVelocity(2.0f);
		
		
		PolygonShape fixtureShape = new PolygonShape();
		fixtureShape.set(vertices2);
    	body.createFixture(fixtureShape, 0.0f);
		
		gameLogicController.getPhysicsWorld().registerPhysicsConnector(new PhysicsConnector(carouselManager, body));
		
		gameLogicController.getScene().attachChild(carouselManager);
	}

	public void addSword(float x, float y){
		SwordManager swordManager = new SwordManager(x, y, 32, 128,
				gameLogicController.mSwordTextureRegion);

		final float width = swordManager.getWidthScaled() / PIXEL_TO_METER_RATIO_DEFAULT;
		final float height = swordManager.getHeightScaled() / PIXEL_TO_METER_RATIO_DEFAULT;

		final Vector2[] vertices = {
				new Vector2(-0.50441f*width, -0.29300f*height),
				new Vector2(-0.00246f*width, -0.49687f*height),
				new Vector2(+0.45494f*width, -0.29118f*height),
				new Vector2(+0.04067f*width, +0.49556f*height)
		};
		
		Body body = PhysicsFactory.createPolygonBody(gameLogicController.getPhysicsWorld(), swordManager, vertices,
				BodyType.KinematicBody, wallFixtureDefF);

			
		body.setAngularVelocity(0.75f);
		
		gameLogicController.getPhysicsWorld().registerPhysicsConnector(new PhysicsConnector(swordManager, body));
		
		gameLogicController.getScene().attachChild(swordManager);
		
		enemyList.add(swordManager);
	}
	
	public void addCannon(float x, float y){
		/*CannonManager carouselManager = new CannonManager(300, 200, 64, 32, 90.0f);
		
		PhysicsFactory.createBoxBody(this.mPhysicsWorld, carouselManager, BodyType.StaticBody, wallFixtureDefF);
				
		scene.attachChild(carouselManager);
		*/
	}

	public void addSpikes(float x, float y, ShapeDirection direction){
		SpikesManager spikesManager = new SpikesManager(x, y, 32, 16, direction, gameLogicController.mSpikesTextureRegion);
			
		gameLogicController.getScene().attachChild(spikesManager);
		enemyList.add(spikesManager);
	}

	public void addTrampoline(float x, float y, ShapeDirection rotation){
		TrampolineManager trampolineManager = new TrampolineManager(x, y, 32, 32, rotation, gameLogicController.mTrampolineTextureRegion);
		
		PhysicsFactory.createBoxBody(gameLogicController.getPhysicsWorld(), trampolineManager, BodyType.StaticBody,
				PhysicsFactory.createFixtureDef(0, 5.0f, 0.0f));
						
		gameLogicController.getScene().attachChild(trampolineManager);
	}
	
	public void addEnemy(Path path, int speed){
		//final FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(3, 0.0f, 0.0f);
		EnemyManager enemy = new EnemyManager(200, 200, 30, 30, gameLogicController.enemyTextureRegion);
		//PhysicsFactory.createCircleBody(this.mPhysicsWorld, enemy, BodyType.StaticBody, fixtureDef);
		enemy.addPath(path, speed);
		gameLogicController.getScene().attachChild(enemy);
		enemyList.add(enemy);
	}
	
	public void addHole(float x, float y){
		EnemyManager enemy = new EnemyManager(x, y, 25, 25, gameLogicController.mHoleTextureRegion);
		gameLogicController.getScene().attachChild(enemy);
		enemyList.add(enemy);
	}
	
	public void addGoods(float x, float y){
		Sprite good = new Sprite(x, y, 30, 30, gameLogicController.mDiamantTextureRegion);
		gameLogicController.getScene().attachChild(good);
		goodsList.add(good);
	}
	
	public void createFrame() {
		final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
		ground.setColor(0.7f, 0.5f, 0.3f);
		final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
		roof.setColor(0.7f, 0.5f, 0.3f);
		final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
		left.setColor(0.7f, 0.5f, 0.3f);
		final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
		right.setColor(0.7f, 0.5f, 0.3f);

		final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.0f, 0.5f);
		PhysicsFactory
				.createBoxBody(gameLogicController.getPhysicsWorld(), ground, BodyType.StaticBody, wallFixtureDef);
		PhysicsFactory.createBoxBody(gameLogicController.getPhysicsWorld(), roof, BodyType.StaticBody, wallFixtureDef);
		PhysicsFactory.createBoxBody(gameLogicController.getPhysicsWorld(), left, BodyType.StaticBody, wallFixtureDef);
		PhysicsFactory.createBoxBody(gameLogicController.getPhysicsWorld(), right, BodyType.StaticBody, wallFixtureDef);

		gameLogicController.getScene().attachChild(ground);
		gameLogicController.getScene().attachChild(roof);
		gameLogicController.getScene().attachChild(left);
		gameLogicController.getScene().attachChild(right);
	}

	public void loadLevel1(){
		addGoods(292, 71);
		addGoods(43, 177);
		addGoods(43, 144);
		addGoods(43, 111);
		addGoods(33, 217);
		addGoods(33, 74);

		addBoxShape(2, 255, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 2, 16, 16, gameLogicController.mBlockTextureRegion);
		addBoxShape(395, 255, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(395, 1, 64, 64, gameLogicController.mBlockTextureRegion);
	
		Path path = new Path(3).to(203, 286).to(205, 2).to(203, 286);
		addEnemy(path, 5);

		if (gameLogicController.getSettingsMenu().getDifficultyLevel() >= LEVEL_MEDIUM) {
			path = new Path(3).to(233, 286).to(235, 2).to(233, 286);
			addEnemy(path, 7);
		}

		if (gameLogicController.getSettingsMenu().getDifficultyLevel() == LEVEL_HARD) {
			path = new Path(3).to(263, 286).to(265, 2).to(263, 286);
			addEnemy(path, 9);
		}


		
	//	addSpikes(100, 10, ShapeRotation.Left);
	//	addSpikes(50, 200, ShapeRotation.Up);
	//	addSpikes(20, 300, ShapeRotation.Down);
	
	//	addCarousel(220, 150);

	//	addSword(220, 150);
		
		addHole(220, 150);
		
	//	addSpikes(220, 150, ShapeRotation.Up);
	//	addTrampoline(220, 150, ShapeRotation.Up);
		
		createEndPoint(426, 139, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 30, 100);

	}
	
	public void loadLevel2(){
		addBoxShape(2, 255, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 191, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 2, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 65, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(65, 254, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(65, 2, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(395, 255, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(395, 192, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(395, 66, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(331, 254, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(331, 2, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(395, 2, 64, 64, gameLogicController.mBlockTextureRegion);
		
		addScores();

		Path path = new Path(3).to(82, 68).to(82, 220).to(82, 68);
		addEnemy(path, 5);
		path = new Path(3).to(163, 2).to(163, 283).to(163, 2);
		addEnemy(path, 5);
		
		if (gameLogicController.getSettingsMenu().getDifficultyLevel() >= LEVEL_MEDIUM) {
			path = new Path(3).to(207, 283).to(206, 2).to(207, 283);
			addEnemy(path, 5);
			path = new Path(3).to(250, 2).to(250, 283).to(250, 2);
			addEnemy(path, 5);
		}
		
		if (gameLogicController.getSettingsMenu().getDifficultyLevel() == LEVEL_HARD) {
			path = new Path(3).to(289, 283).to(289, 2).to(289, 283);
			addEnemy(path, 5);
			path = new Path(3).to(350, 68).to(350, 220).to(350, 68);
			addEnemy(path, 5);
		}

		createEndPoint(425, 145, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 2, 145);
	}
	
	public void loadLevel3_1(){

		addBoxShape(0, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(32, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(64, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(96, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(128, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(160, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(192, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(224, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(256, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(288, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(320, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(352, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(384, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(416, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(448, 0, 32, 32, gameLogicController.mBlockTextureRegion);
		
		addBoxShape(0, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(32, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(64, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(96, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(128, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(160, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(192, 180, 32, 24, gameLogicController.mBlockTextureRegion);
		addBoxShape(224, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(256, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(288, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(320, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(352, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		// addBoxShape(384, 180, 32, 32,
		// gameLogicController.mBlockTextureRegion);
		
		addBoxShape(0, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(32, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(64, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(96, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(128, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(160, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		//addBoxShape(192, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(224, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(256, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(288, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(320, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(352, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(384, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(416, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(448, 288, 32, 32, gameLogicController.mBlockTextureRegion);
		
		addScores();
		
		addSword(110, 42);

		addGoods(180, 50);
		addGoods(260, 85);
		addGoods(300, 235);
		addGoods(100, 215);
		
		
		addHole(225, 138);
		
		if (gameLogicController.getSettingsMenu().getDifficultyLevel() >= LEVEL_MEDIUM) {
			addHole(360, 45);
			addSpikes(416, 180, ShapeDirection.Up);
			addSpikes(416, 196, ShapeDirection.Down);
		}
		else{
			addBoxShape(416, 180, 32, 32, gameLogicController.mBlockTextureRegion);
		}
			

		if (gameLogicController.getSettingsMenu().getDifficultyLevel() == LEVEL_HARD) {
			addHole(345, 140);
		}
		
		addSpikes(192, 204, ShapeDirection.Down);
		addTrampoline(192, 288, ShapeDirection.Up);
		
		createEndPoint(4, 237, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 2, 75);
	}
	
	public void loadLevel3(){

		addGoods(161, 148);
		addGoods(311, 148);

		addBoxShape(0, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 64, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 128, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 192, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		
		addBoxShape(64, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(128, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(192, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(256, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(320, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(384, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		
		addBoxShape(64, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(128, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(192, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(256, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(320, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(384, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		
		addScores();

		Path path = new Path(7).to(128, 222).to(188, 72).to(258, 222).to(347, 68).to(258,222).to(188, 72).to(128,222);
		addEnemy(path,10);
		
		path = new Path(9).to(383, 222).to(312, 70).to(224, 221).to(155, 71).to(143,217).to(155,71).to(224, 221).to(312,70).to(383, 222);
		addEnemy(path,10);
		
		createEndPoint(425, 156, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 68, 220);
	}
	
	public void loadLevel4(){
		addBoxShape(2, 255, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 1, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 65, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 128, 64, 64, gameLogicController.mBlockTextureRegion);
		
		Path path = new Path(6).to(122, 48).to(84, 149).to(106, 247).to(238, 261).to(241, 37).to(122,48);
		addEnemy(path,2);

		createEndPoint(2, 209, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 248, 163);

	}
	
	public void loadLevel5() {
		addGoods(143, 200);
		addGoods(143, 92);

		addBoxShape(0, 257, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 193, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 64, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 161, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(0, 128, 64, 64, gameLogicController.mBlockTextureRegion);

		addBoxShape(64, 193, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(64, 257, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(64, 64, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(64, 0, 64, 64, gameLogicController.mBlockTextureRegion);

		addBoxShape(128, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(192, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(256, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(320, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(383, 256, 64, 64, gameLogicController.mBlockTextureRegion);

		addBoxShape(128, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(192, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(255, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(319, 0, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(383, 0, 64, 64, gameLogicController.mBlockTextureRegion);

		addBoxShape(188, 122, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(316, 122, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(393, 122, 64, 64, gameLogicController.mBlockTextureRegion);

		Path path = new Path(3).to(267, 217).to(267, 68).to(267, 217);
		addEnemy(path, 5);

		createEndPoint(410, 220, 32, 32);
		createEndPoint(410, 84, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 29, 145);
	}

	public void loadLevel6(){
		addGoods(82, 159);
		addGoods(222, 156);
		addGoods(378, 111);

		addBoxShape(1,1 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(1,65 , 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(1,97 , 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(1,129 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(1,193 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(1,257 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(133,1 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(133,64 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(133, 128 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(133, 192 , 64, 64, gameLogicController.mBlockTextureRegion);
		
		addBoxShape(274, 255 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(274, 192 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(274, 127 , 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(274, 63 , 64, 64, gameLogicController.mBlockTextureRegion);
		
		addBoxShape(427, 219 , 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(338, 218 , 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(348, 250 , 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(410, 251, 32, 32, gameLogicController.mBlockTextureRegion);
		
		Path path = new Path(7).to(69, 5).to(99, 141).to(67, 239).to(147, 287).to(67, 239).to(99, 141).to(69, 5);
		addEnemy(path,5);
		
		path = new Path(7).to(147, 287).to(241, 206).to(198, 84).to(287, 5).to(198, 84).to(241, 206).to(147, 287);
		addEnemy(path,5);
		
		path = new Path(3).to(339, 69).to(427, 69).to(339, 69);
		addEnemy(path,2);
		
		createEndPoint(379, 251, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 34, 82);
	}
	
	public void loadLevel7() {
		addGoods(210, 174);

		addBoxShape(2, 2, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 128, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 256, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(2, 192, 64, 64, gameLogicController.mBlockTextureRegion);
		addBoxShape(396, 64, 16, 256, gameLogicController.mBlockTextureRegion);

		Path path = new Path(3).to(98, 303).to(95, 15).to(98, 303);
		addEnemy(path, 5);

		path = new Path(3).to(147, 76).to(167, 300).to(147, 76);
		addEnemy(path, 5);

		path = new Path(3).to(121, 215).to(246, 67).to(121, 215);
		addEnemy(path, 5);

		path = new Path(3).to(90, 13).to(226, 13).to(90, 13);
		addEnemy(path, 5);

		path = new Path(3).to(206, 127).to(70, 17).to(206, 127);
		addEnemy(path, 5);

		path = new Path(5).to(288, 215).to(150, 124).to(255, 10).to(241, 110).to(288, 215);
		addEnemy(path, 5);

		createEndPoint(420 - 2, 280 - 2, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 30, 100);
	}

	public void loadLevel8() {
		addGoods(102, 60);
		addGoods(372, 64);
		addGoods(352, 211);
		addGoods(84, 253);
		addGoods(195, 253);
		
		addBoxShape(1, 60, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(32, 60, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(64, 60, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(139, 60, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(171, 60, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(203, 60, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(234, 60, 32, 32, gameLogicController.mBlockTextureRegion);
		
		addBoxShape(234, 166, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(203, 166, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(172, 166, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(140, 166, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(108, 166, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(76, 166, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(45, 166, 32, 32, gameLogicController.mBlockTextureRegion);
		
		addBoxShape(45, 198, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(45, 230, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(45, 261, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(45, 293, 32, 32, gameLogicController.mBlockTextureRegion);
		
		Path path = new Path(3).to(103, 3).to(102, 125).to(103, 3);
		addEnemy(path, 2);

		path = new Path(7).to(420, 7).to(300, 108).to(420, 209).to(300, 274).to(300, 108).to(420, 209).to(420, 7);
		addEnemy(path, 5);

		path = new Path(3).to(234, 200).to(234, 286).to(234, 200);
		addEnemy(path, 3);

		path = new Path(3).to(140, 200).to(140, 286).to(140, 200);
		addEnemy(path, 3);

		createEndPoint(5, 288, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 2, 2);
	}

	public void loadLevel9() {
		addGoods(232, 205);
		addGoods(322, 116);
		addGoods(321, 205);
		addGoods(147, 205);
		addGoods(147, 119);
		addGoods(235, 119);

		addBoxShape(96, 66, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(96, 153, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(96, 237, 32, 32, gameLogicController.mBlockTextureRegion);

		addBoxShape(181, 66, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(181, 153, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(181, 237, 32, 32, gameLogicController.mBlockTextureRegion);

		addBoxShape(270, 66, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(270, 153, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(270, 237, 32, 32, gameLogicController.mBlockTextureRegion);

		addBoxShape(355, 66, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(355, 153, 32, 32, gameLogicController.mBlockTextureRegion);
		addBoxShape(355, 237, 32, 32, gameLogicController.mBlockTextureRegion);

		Path path = new Path(3).to(51, 109).to(391, 109).to(51, 109);
		addEnemy(path, 4);

		path = new Path(3).to(391, 196).to(51, 196).to(391, 196);
		addEnemy(path, 4);

		path = new Path(3).to(138, 19).to(138, 280).to(138, 19);
		addEnemy(path, 4);

		path = new Path(3).to(232, 19).to(232, 280).to(229, 19);
		addEnemy(path, 4);

		path = new Path(3).to(315, 280).to(315, 19).to(315, 280);
		addEnemy(path, 4);

		createEndPoint(426, 153, 32, 32);
		createPlayer(gameLogicController.mCircleFaceTextureRegion, 2, 153);
	}

	public Body getmPlayerBody() {
		return mPlayerBody;
	}

	public Player getmPlayer() {
		return mPlayer;
	}
	
	public GameLogicController getGameLogicController() {
		return gameLogicController;
	}

	public ArrayList<Shape> getEndPointList() {
		return endPointList;
	}

	public ArrayList<Shape> getGoodsList() {
		return goodsList;
	}

	public ArrayList<Shape> getEnemyList() {
		return enemyList;
	}

	public void setMusic(Music gameMusic, Sound gameOverSound) {
		this.gameMusic = gameMusic;
		this.gameOverSound = gameOverSound;
	}

	private void clearShapesLists() {
		endPointList.clear();
		goodsList.clear();
		enemyList.clear();
	}

	private void addScores() {
		/* The ScoreText showing how many points the pEntity scored. */
		scoreText = new ChangeableText(5, 5, gameLogicController.font, "Time: 0", "Time: XXXXX".length());
		scoreText.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		scoreText.setAlpha(0.5f);
		gameLogicController.getScene().attachChild(scoreText);

		gameLogicController.getScene().registerUpdateHandler(timeHandler);


	}


}
